package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.validator.ValidatorException;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.DeviceType;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.devices.DeviceBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

@ManagedBean(name="deviceInfoAdminBean")
@ViewScoped
public class DeviceInfoAdminBean {
	
	
	@ManagedProperty(value="#{flash.selected}")
	private DeviceBean selected;

	@ManagedProperty(value="#{systemService}")
	private SystemService sysService;

	private Map<String, Integer> comboBoxType;
	private Map<Integer,DeviceType> typesMap;
	
	public void forwardDevicesIfNotSelection(ComponentSystemEvent cse) {
		FacesContext faceContext = FacesContext.getCurrentInstance();
		if (selected == null) {
			faceContext.getApplication().getNavigationHandler().
		handleNavigation(faceContext, null,	"/adminuser/devices?faces-redirect=true");
		}
	}
	
	public DeviceBean getSelected() {
		return selected;
	}

	public void setSelected(DeviceBean selected) {
		this.selected = selected;
	}

	public SystemService getSysService() {
		return sysService;
	}

	public void setSysService(SystemService sysService) {
		this.sysService = sysService;
	}
	
	public Map<String,Integer> getDeviceTypesMap(){
		
		List<DeviceType> types = sysService.getSysDeviceTypes();
		
		comboBoxType = new HashMap<String, Integer>(types.size());
		typesMap = new HashMap<Integer, DeviceType>(types.size());
		
		for (DeviceType deviceType : types) {
			comboBoxType.put(deviceType.getName(), deviceType.getID());
			typesMap.put(deviceType.getID(), deviceType);
		}
		
		return comboBoxType;
		
	}
	
	public Integer getType(){
		if(selected.getDevice().getType() == null)
			return null;
		return selected.getDevice().getType().getID();
	}
	
	public void setType(Integer id){
		selected.getDevice().setType(typesMap.get(id));
	}
	
	public void validateDNSname(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		  String dnsString = (String) value;
		  String msg = "#{msgs['devicedata.baddns']}";
		  if (dnsString == null || !isValidDomainName(dnsString)) {
		    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg));
		  }
		}
	
	private static Pattern pIPaddress;
	private static Pattern pHostName;
	private static final String VALID_IP_ADDRESS_REGEX = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
	private static final String VALID_HOSTNAME_REGEX = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$";
	
	static {
		pIPaddress = Pattern.compile(VALID_IP_ADDRESS_REGEX);
		pHostName = Pattern.compile(VALID_HOSTNAME_REGEX);
	}
 
	public static boolean isValidDomainName(String domainName) {
		return pIPaddress.matcher(domainName).find() || pHostName.matcher(domainName).find() ;
	}
	
}
